package com.school.gift.controller.admin;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.Goods;
import com.school.gift.domain.GoodsImg;
import com.school.gift.model.GoodsModel;
import com.school.gift.service.GoodsImgService;
import com.school.gift.service.GoodsService;
import com.school.support.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.support.response.Response;
import zhibi.common.util.io.FileUtil;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by 执笔 on 2017/3/16.
 */
@Controller
@RequestMapping("admin/goods")
public class MGoodsController extends AdminBaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsImgService goodsImgService;

    @RequestMapping("list")
    public String list(Model model, Page page, GoodsModel goodsModel) {
        Example example = ExampleUtil.getInstance()
                .addParam("g.name", goodsModel.getName(), ExampleType.Operation.LIKE)
                .addOrder("g.addtime")
                .getExample();
        PageInfo<GoodsModel> pageInfo = goodsService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/goods/list";
    }

    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable Integer id) {
        Goods goods = goodsService.selectByPK(id);
        if (null == goods) {
            throw new MessageException("该礼品不存在");
        }
        goods.setStatus((goods.getStatus() + 1) % 2);
        goodsService.updateByIdSelective(goods);
        return refresh();
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.selectByPK(id);
        if (null == goods) {
            throw new MessageException("该礼品不存在");
        }
        List<GoodsImg> goodsImgs = goodsImgService.selectByGoods(id);
        model.addAttribute(goods);
        model.addAttribute(goodsImgs);
        return "admin/goods/detail";
    }

    @RequestMapping("updateGoods")
    public String updateGoods(Goods goods, @RequestParam MultipartFile[] icons,MultipartFile img){
        saveGoodsImg(goods,icons,img);
        return redirect("detail/"+goods.getId());
    }

    @RequestMapping("removeImg")
    @ResponseBody
    public Response<String> removeImg(Integer id){
        goodsImgService.deleteByPK(id);
        return Response.ok("");
    }

    @RequestMapping("add")
    public String add(){
        return "admin/goods/add";
    }

    @RequestMapping("save")
    public String save(Goods goods, @RequestParam MultipartFile[] icons,MultipartFile img){
        goods.setAddtime(new Date());
        goods.setTimes(0);
        goodsService.insertSelective(goods);
        saveGoodsImg(goods,icons,img);
        return redirect("detail/"+goods.getId());
    }

    private void saveGoodsImg(Goods goods, MultipartFile[] icons, MultipartFile img){
        if(icons.length>0){
            for(MultipartFile file : icons){
                if(!file.isEmpty()){
                    GoodsImg goodsImg = new GoodsImg();
                    goodsImg.setAddtime(new Date());
                    goodsImg.setGoodsId(goods.getId());
                    goodsImg.setName(file.getOriginalFilename());
                    try {
                        goodsImg.setPath(FileUtil.save(filePath(),file));
                    } catch (IOException e) {
                        logger.error(e.getMessage());
                    }
                    goodsImgService.insertSelective(goodsImg);
                    goods.setIcon(goodsImg.getPath());
                }
            }
        }
        if(null != img && !img.isEmpty()){
            try {
                goods.setFilePath(FileUtil.save(filePath(),img));
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
        goodsService.updateByIdSelective(goods);
    }
}
